/**
 * 2017年3月9日
 * 大厅
 */

var pomelo = require('pomelo');

var playerUtil = require('../util/player');
var utils = require('../../util/utils');

var log = console.log;

function Lobby(playerManager) { 

    this.playerManager = playerManager;

    this.gameId = 5; // 二人牛牛
    this.playerData = {};
    this.playerNum = {};
    this.roomData = {};  // 房间玩家信息容器

    this.id = 1;
    this.name = this.gameId + '_' + this.id;

    this.channelService = pomelo.app.get('channelService');    
    this.createChannel();
}

var pro = Lobby.prototype;

pro.createChannel = function () {
    if (this.channel) {
        return this.channel;
    }
    var channelName = this.name + '_lobby';
    this.channel = this.channelService.getChannel(channelName, true);

    return this.channel;
};

pro.addPlayer = function (msg, callback) {
    log('用户进入大厅');

    // msg = {
    //     playerId: '1004',
    //     token: 'abbc75c0e8434c1fab56aabb05b0d65c',
    //     gameId: 5,
    //     __route__: 'connector.entryHandler.enterGame',
    //     serverId: 'connector-server-1',
    //     isRobot: false,
    //     ip: '::ffff:127.0.0.1'
    // };

    var self = this;
    var playerId = msg.playerId;

    playerUtil.playerLogin(msg, function (err, rel) {
        if (err) {
            console.log(err);
            callback(err);
        }
        else {
            player = rel;
            self.playerData[playerId] = player;
            player.isRobot = false;

            self.playerNum++;
            
            self.channel.add(msg.playerId, msg.serverId);            
            self.channel.pushMessage('onGameEnter', player);

            var ret = {};
            ret.gameId = self.gameId;        
            ret.playerData = self.getPlayerData(playerId);
            ret.playerList = self.getPlayerList();
            ret.roomData = self.getRoomData();
            log(ret);
            callback(null, ret);
        }
    });
};

// 获取当前用户名单
pro.getPlayerList = function () {
    log('获取当前用户名单!');
    var self = this;
    var playerList = [];
    var player;

    for (var playerId in this.playerData) {
        player = this.playerData[playerId];
        playerList.push({
            playerId: player.playerId,
            playerName: player.playerName,
            coin: player.coin
        });
    }
    return playerList;
};

// 获取当前用户信息
pro.getPlayerData = function (playerId) {
    log('获取当前用户信息： ', playerId);

    var playerData = {}, player;

    if (this.playerData[playerId]) {
        player = this.playerData[playerId];
        playerData = {
            playerId: playerId,
            playerName: player.playerName,
            coin: player.coin,
            score: player.score,
            roundCount: player.roundCount
        };
    }
    return playerData;
};

// 把用户添加到房间信息中
pro.addPlayerToRoom = function (playerId, rid, type) {
    log('添加用户到大厅的房间信息中');

    if (!this.roomData[playerId]) {
        this.roomData[playerId] = { rid, type };
    }

};

// 从房间中删除用户
pro.delPlayerFromRoom = function (playerId) {
    log('从房间中删除用户');

    if (this.roomData[playerId]) {
        delete this.roomData[playerId];
    }

};

// 检查用户是否在房间中
pro.checkPlayerInRoom = function (playerId) {
    log('检查用户是否在房间中');
    if (this.roomData[playerId]) {
        return true;
    }
    return false;
};

// 获取房间用户信息
pro.getRoomData = function () {
    return this.roomData;
};

// -------------------------------------------------------------

// 用户从大厅退出
pro.kickPlayer = function (msg) {
    log('大厅用户 %s 退出', msg.playerId);

    var playerId = msg.playerId;
    var serverId = msg.serverId;

    this.delPlayerFromRoom(playerId);

    this.channel.leave(playerId, serverId);
    // delete this.playerData[playerId]; // 这时还不能直接删掉用户，还得看用户是不是在房间里

    this.channel.pushMessage('onPlayerLeave', { playerId });
};

// 删掉内存中的用户
pro.deletePlayer = function (msg) {
    var playerId;
    if (typeof msg === 'string') {
        playerId = msg;
    }
    else {
        playerId = msg.playerId;
    }
    if (!isNaN(playerId) && this.playerData[playerId]) {
        delete this.playerData[playerId];
    }
};




/**
 * Expose 'lobby' constructor.
 */
module.exports = Lobby;
